// pages/vipCard/updateBlackCard/updateBlackCard.js
const app = getApp();
Page({


  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl: app.globalData.apiImgUrl,
    checked:true,
    interestList: [], //享受的权益
    interest: {},
    RelaId: 0, //车主卡id
    savingMoney: 6988, //节省金额
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this;
    self.setData({
      RelaId: options.RelaId
    })
    self.getCardContent();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * 支付
   */
  payTap: function() {
    var self = this;
    //根据授权码进行用户添加
    var url = app.globalData.apiRootUrl + '/WxPay/TradeMiniPay';
    if (self.data.checked) {
      app.httpRequest({
        url: url,
        showLoading: false,
        method: "GET",
        data: {
          businessId: self.data.RelaId,
          openId: app.globalData.userInfo.OpenID
        },
        success(res) {
          var rdata = res.data;
          wx.requestPayment({
            timeStamp: rdata.data.timeStamp,
            nonceStr: rdata.data.nonceStr,
            package: rdata.data.package,
            signType: rdata.data.signType,
            paySign: rdata.data.paySign,
            success: function(res) {
              app.globalData.userInfo.CardLevel = 2;
              app.setUserInfo(app.globalData.userInfo);
              wx.switchTab({
                url: '/pages/vipCard/vipCard',
              })
            },
            fail: function(res) {
              wx.showModal({
                showCancel: false,
                title: '提示',
                content: '订单支付失败',
                success() {
                  wx.switchTab({
                    url: '/pages/vipCard/vipCard',
                  })
                }
              });
            }
          })
        }
      });
    } else {
      wx.showToast({
        title: '请先同意服务协议',
        icon: 'none'
      })
    }

  },
  /**
   * 获取卡片内容
   */
  getCardContent: function() {
    let self = this;
    let url = app.globalData.apiRootUrl + '/UserCard/GetCardContents';
    app.httpRequest({
      url: url,
      data: {
        level: 2
      },
      success: function(res) {
        let rdata = res.data;
        if (rdata.status == 0 && rdata.data.Contents.length > 0) {
          self.setData({
            interestList: rdata.data.Contents,
            interest: rdata.data
          })
        }
      },
      fail: function(err) {

      }
    })
  },
  /**
   * 跳转到协议
   */
  goToAgreementTap: function() {
    wx.navigateTo({
      url: '/pages/vipCard/agreement/agreement',
    })
  },
  /**
   * 选择同意协议
   */
  checkTap: function() {
    let self = this;
    self.setData({
      checked: !self.data.checked
    })
  }
})